using System;
using System.Collections.Generic;
using System.Linq;
using TLDRML.Tokens;

namespace TLDRML.Nodes
{
    public class Node
    {
        public virtual IEnumerable<Token> Parse(IEnumerable<Token> tokens)
        {
            return tokens;
        }


        protected IEnumerable<Token> Expect<T>(IEnumerable<Token> tokens, Action<T> action) where T : Token
        {
            Token token = tokens.First();
            var expectedToken = token as T;

            if (expectedToken == null)
                throw new InvalidOperationException(String.Format("Unexpected token at {0}", token.Position));

            action(expectedToken);

            return tokens.Skip(1);
        }

        protected IEnumerable<Token> Parse<T>(IEnumerable<Token> tokens, Action<T> action) where T : Node, new()
        {
            var node = new T();

            tokens = node.Parse(tokens);

            action(node);

            return tokens;
        }

        protected static void DiscardToken(Token token)
        {
        }
    }
}